package com.southwind.controller;


import com.southwind.common.TreeNode;
import com.southwind.entity.SysMenu;
import com.southwind.service.SysMenuService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author admin
 * @since 2024-05-28
 */
@RestController
@RequestMapping("/menu")
public class SysMenuController {

    @Autowired
    private SysMenuService menuService;

    @GetMapping("/leftMenu")
    public List<TreeNode> leftMenu(){
        List<SysMenu> list = this.menuService.list();
        List<TreeNode> result = new ArrayList<>();
        for (SysMenu menu : list) {
            //一级菜单
            if(menu.getPid() == 1) {
                TreeNode parent = new TreeNode();
                BeanUtils.copyProperties(menu, parent);
                result.add(parent);
                List<TreeNode> children = new ArrayList<>();
                //二级菜单
                for (SysMenu sysMenu : list) {
                    if(sysMenu.getPid() == parent.getId()){
                        TreeNode child = new TreeNode();
                        BeanUtils.copyProperties(sysMenu, child);
                        children.add(child);
                    }
                }
                parent.setChildren(children);
            }
        }
        return result;
    }

}

